
import java.util.EmptyStackException;
import java.util.Stack;;

public class QueueUsingStacks {
	
	static class Queue<T extends Comparable> {
		private Stack<T> leftStack = new Stack<T>();
		private Stack<T> rightStack = new Stack<T>();
		
		public T poll() {
			if(rightStack.size() == 0) {
				move();
			}
			
			T t = null;
			try {
				t = rightStack.pop();
			} catch(EmptyStackException e) {
				
			}
			return t;
		}
		
		public void offer(T t) {
			leftStack.push(t);
		}
		
		public T peek() {
			if(rightStack.size() == 0) {
				move();
			}
			
			return rightStack.peek();
		}
		
		private void move() {
			while(leftStack.size() > 0) {
				rightStack.push(leftStack.pop());
			}
		}
		
		public int size() {
			return leftStack.size() + rightStack.size();
		}
	}
	
	public static void main(String []args) {
		
		Queue<Integer> queue = new Queue<Integer>();
		queue.offer(5);
		System.err.println(queue.poll());
		
		queue.offer(15);
		queue.offer(25);
		queue.offer(35);
		
		System.err.println(queue.poll());
		System.err.println(queue.peek());
		
		queue.offer(45);
		queue.offer(55);
		
		System.err.println(queue.poll());
		System.err.println(queue.poll());
		System.err.println(queue.poll());
		System.err.println(queue.poll());
		System.err.println(queue.poll());
		
	}

}
